package com.szh.zx.duoxiancheng.chapter1;
/**
 * 线程协作-同时开始
 *
 * */
public class FireFlag {

    private volatile boolean fire = false;

    public synchronized void waitForFire() throws InterruptedException {
        while (!fire) {
            wait();
        }
    }

    public synchronized void fire() {
        this.fire = true;
        notifyAll();
    }

    public static void main(String[] args) throws InterruptedException {
        int num=10;
        FireFlag fireFlag = new FireFlag();
        Thread[] recers =new Thread[num];
        for (int i = 0; i < num; i++) {
            recers[i] = new Racer(fireFlag);
            recers[i].start();
        }
        Thread.sleep(1_000);
        fireFlag.fire();
    }

    static class Racer extends Thread {
        FireFlag fireFlag;

        public Racer(FireFlag fireFlag) {
            this.fireFlag = fireFlag;
        }

        @Override
        public void run() {
            try {
                this.fireFlag.waitForFire();
                System.out.println("start run "+Thread.currentThread().getName());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }
}

